package com.qianbao.qianbaobusiness.ui.mine.personalinfo;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.baseandroid.config.Global;
import com.baseandroid.event.LoginInvalideEvent;
import com.baseandroid.mvp.BaseMvpActivity;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.UserInfo;
import com.qianbao.qianbaobusiness.ui.mine.personalinfo.email.EmailSettingActivity;
import com.qianbao.qianbaobusiness.ui.widget.dialog.ConfirmDialog;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

public class PersonalInfoActivity extends BaseMvpActivity<PersonalInfoView, PersonalInfoPresenter> implements PersonalInfoView {

    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_role)
    TextView mTvRole;
    @BindView(R.id.tv_mobile)
    TextView mTvMobile;
    @BindView(R.id.tv_email)
    TextView mTvEmail;
    @BindView(R.id.tv_certificate_type)
    TextView mTvCertificateType;
    @BindView(R.id.tv_certificate_no)
    TextView mTvCertificateNo;
    private UserInfo.User user;
    private ConfirmDialog mConfirmDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_personal_info;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void setupView() {
        mTvTitle.setText("个人信息");
        user = Global.getUserInfoBean().getUser();
        mTvName.setText(user.getUserName());
        mTvRole.setText(user.getPositionName());
        mTvMobile.setText(user.getMobile());
        mTvEmail.setText(user.getEmail());
        mTvCertificateType.setText(user.getCertName());
        String certNo = "";
        if (!TextUtils.isEmpty(user.getCertNo())) {
            certNo = user.getCertNo().replaceAll("(\\d{4})(\\d+)(\\d{4})", "$1**********$3");
        }
        mTvCertificateNo.setText(certNo);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setupView();
    }

    @Override
    protected void setupData(Bundle savedInstanceState) {

    }

    @OnClick({R.id.iv_back_toolbar, R.id.tv_email, R.id.tv_quit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back_toolbar:
                finish();
                break;
            case R.id.tv_email:
                Intent emailIntent = new Intent(this, EmailSettingActivity.class);
                startActivity(emailIntent);
                break;
            case R.id.tv_quit:
                ConfirmDialog.Builder builder = new ConfirmDialog.Builder(this);
                mConfirmDialog = builder.setContentText("退出登录将无法接收到买单通知等推送消息，确认退出吗？").setConfirmListener(new ConfirmDialog.Builder.ConfirmClickListener() {
                    @Override
                    public void click(boolean b) {
                        mConfirmDialog.dismiss();
                        if (b) {
                            getPresenter().updatePushAudience();
                        }
                    }
                }).create();
                mConfirmDialog.show();
                break;
            default:
                break;
        }
    }

    @Override
    public void logoutSuccess() {
        EventBus.getDefault().post(new LoginInvalideEvent());
    }

    @Override
    public void onNetError() {

    }
}
